package com.cqupt.sendMes.exception;

import com.cqupt.sendMes.enums.HttpStatusEnum;
import com.cqupt.sendMes.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.FieldError;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@ControllerAdvice
@Slf4j
public class GlobalException {

    /**
     * 全局异常
     * */
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public Result<String> exceptionHandler(RuntimeException e){
        log.error("捕获全局异常: {}",  e.getMessage(), e);
        return Result.createFailResult(HttpStatusEnum.UNKOWN_SYSTEM_ERROR.getCode(), HttpStatusEnum.UNKOWN_SYSTEM_ERROR.getMessage());
    }

    /**
     * 自定义业务异常
     * */
    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result<String> BusinessExceptionHandler(BusinessException e){
        log.error("捕获到业务异常: {}", e.getMessage());
        return Result.createFailResult(e.getCode(), e.getMessage());
    }

    /**
     * 字段验证不通过异常
     * */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public Result<String> MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){
        List<FieldError> fieldErrorList =  e.getBindingResult().getFieldErrors();

        //提示一条数据错误
        String  message = fieldErrorList.get(0).getDefaultMessage();

        log.error("参数校验异常: {}", message);
        return Result.createFailResult(HttpStatusEnum.PARAM_INPUT_INVALID.getCode(), HttpStatusEnum.PARAM_INPUT_INVALID.getMessage() +  message);
    }

    /**
     * Param参数丢失异常
     */
    @ExceptionHandler(MissingServletRequestParameterException.class)
    @ResponseBody
    public Result<String> MissingServletRequestParameterExceptionHandler(MissingServletRequestParameterException e){
        String message = e.getMessage();
        log.error("参数丢失异常: {}", message);
        return Result.createFailResult(HttpStatusEnum.PARAM_INPUT_INVALID.getCode(), HttpStatusEnum.PARAM_INPUT_INVALID.getMessage());
    }

    /**
     * 空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseBody
    public Result<String> NullPointerExceptionHandler(NullPointerException e){
        String message = e.getMessage();
        log.error("空指针异常: {}", message);
        return Result.createFailResult(HttpStatusEnum.UNKOWN_SYSTEM_ERROR.getCode(), HttpStatusEnum.UNKOWN_SYSTEM_ERROR.getMessage());
    }

    /**
     * 请求方式错误
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Result<String> HttpRequestMethodNotSupportedExceptionHandler(HttpRequestMethodNotSupportedException e){
        String message = e.getMessage();
        log.error("请求方式错误: {}", message);
        return Result.createFailResult(HttpStatusEnum.HTTP_METHOD_ERROR.getCode(), HttpStatusEnum.HTTP_METHOD_ERROR.getMessage());
    }

}
